/**
 * Copyright (c) 2010 David Hamrick, Guy Kopsombut, Emily Ross 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "CurrentFareVC.h"
//#import "User.h"

#define YELLOWCOLOR [UIColor colorWithRed:1.0 green:1.0 blue:.6 alpha:1.0]

@implementation CurrentFareVC

enum AlertViewButtons
{
	AlertViewCancelButton,
	AlertViewOkButton
};

- (id)initWithStyle:(UITableViewStyle)style
{
	if(self = [super initWithStyle:style])
	{
		statusChoices = [[NSArray arrayWithObjects:@"Driver Assigned",
																 @"Driver En Route",
																 @"Driver Arrived",
																 @"Passenger Picked Up",
																 @"Passenger Dropped Off",nil] retain];
	}
	return self;
}

- (void)viewDidLoad
{		
	passenger = [[[UIApplication sharedApplication] delegate] assignedPassenger];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	switch(section)
	{
		case 0:
			return 3;
		case 1:
			return [statusChoices count];
		default:
			return 0;
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
	}
	
	
	switch(indexPath.section)
	{
		case 0:
			switch(indexPath.row)
			{
				case 0:
					cell.textLabel.text = @"Name";
					cell.detailTextLabel.text = [passenger name];
					break;
				case 1:
					cell.textLabel.text = @"Address";
					cell.detailTextLabel.text = [passenger address];
					break;
				case 2:
					cell.textLabel.text = @"Phone";
					cell.detailTextLabel.text = [passenger phone];
					break;
			}
			break;
		case 1:
			cell.textLabel.text = [statusChoices objectAtIndex:indexPath.row];
			
			if(indexPath.row <= [passenger statusNum])
			{
				cell.backgroundColor = YELLOWCOLOR;
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
			}
			else
			{
				cell.backgroundColor = [UIColor whiteColor];
				cell.accessoryType = UITableViewCellAccessoryNone;
			}
			break;
	}

	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{	
	[passenger setStatusNum:indexPath.row];
	[passenger setStatus:[statusChoices objectAtIndex:indexPath.row]];
	[passenger save];
	
	if([passenger statusNum] == [statusChoices count]-1)
	{
		[self showPassengerDone];
	}
	
	[self.tableView reloadData];
}

- (void)showPassengerDone
{
	UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Passenger Dropped Off?"
																		 message:nil
																		delegate:self
															cancelButtonTitle:@"Cancel"
															otherButtonTitles:@"Accept",nil];
	[alertView show];
	
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if(buttonIndex==AlertViewCancelButton)
	{
		return;
	}
	[passenger delete];
	[[[UIApplication sharedApplication] delegate] setAssignedPassenger:nil];
	
	[self.navigationController popViewControllerAnimated:YES];
}

@end
